﻿@model MessageTemplateModel

@using SmartStore.Web.Framework.UI

@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)

<script>
	$(function () {
		$("#template-edit").on('shown.bs.tab', 'a[data-toggle="tab"]', function (e) {
			// Refresh/repaint hidden CodeMirror instance
			var a = $(e.target);
			var el = a.closest('.nav').next().find(a.attr('href') + ' .CodeMirror');
			if (el.length) {
				el[0].CodeMirror.refresh();
			}
			
		});
	});
</script>

@Html.SmartStore().TabStrip().Name("template-edit").Style(TabsStyle.Material).Position(TabsPosition.Top).Items(x =>
{
	x.Add().Text(T("Admin.Common.Info").Text).Content(TabInfo()).Selected(true);
	x.Add().Text(T("Admin.Common.Stores").Text).Content(TabStores());
    
    // generate an event
	EngineContext.Current.Resolve<IEventPublisher>().Publish(new TabStripCreated(x, "template-edit", this.Html, this.Model));
})

@helper TabInfo()
{
	string modelTree = ViewBag.LastModelTreeJson;
	string templateName = "MessageTemplate/" + Model.Name;

	if (modelTree.HasValue())
	{
		<script>
			$(function () {
				SmartStore.Admin.modelTrees["@Html.Raw(templateName)"] = @Html.Raw("JSON.parse('" + modelTree + "')");
			});
		</script>
	}

	<table class="adminContent mb-2">
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.Name)
            </td>
            <td class="adminData">
				<div class="form-control-plaintext">
					@Model.Name
				</div>
                @Html.HiddenFor(model => model.Name)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.IsActive)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.IsActive)
                @Html.ValidationMessageFor(model => model.IsActive)
            </td>
        </tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.SendManually)
			</td>
			<td class="adminData">
				@Html.EditorFor(model => model.SendManually)
				@Html.ValidationMessageFor(model => model.SendManually)
			</td>
		</tr>
    </table>
	@(Html.LocalizedEditor<MessageTemplateModel, MessageTemplateLocalizedModel>("messagetemplate-localized",
    @<table class="adminContent">
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.Locales[item].EmailAccountId)
			</td>
			<td class="adminData">
				@*IMPORTANT: Do not delete, this hidden element contains the id to assign localized values to the corresponding language *@
				@Html.HiddenFor(model => model.Locales[item].LanguageId)

				@Html.DropDownListFor(model => Model.Locales[item].EmailAccountId, new SelectList(Model.AvailableEmailAccounts, "Id", "DisplayName", Model.Locales[item].EmailAccountId))
				@Html.ValidationMessageFor(model => model.Locales[item].EmailAccountId)
			</td>
		</tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.Locales[item].To)
            </td>
            <td class="adminData">
                @Html.TextBoxFor(model => Model.Locales[item].To)
                @Html.ValidationMessageFor(model => model.Locales[item].To)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.Locales[item].ReplyTo)
            </td>
            <td class="adminData">
                @Html.TextBoxFor(model => Model.Locales[item].ReplyTo)
                @Html.ValidationMessageFor(model => model.Locales[item].ReplyTo)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.Locales[item].BccEmailAddresses)
            </td>
            <td class="adminData">
                @Html.TextBoxFor(model => Model.Locales[item].BccEmailAddresses)
                @Html.ValidationMessageFor(model => model.Locales[item].BccEmailAddresses)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.Locales[item].Subject)
            </td>
            <td class="adminData">
                @Html.TextBoxFor(model => Model.Locales[item].Subject)
                @Html.ValidationMessageFor(model => model.Locales[item].Subject)
            </td>
        </tr>
	 	<tr>
	 		<td class="adminTitle">
	 			@Html.SmartLabelFor(model => model.Locales[item].Attachment1FileId)
	 		</td>
	 		<td class="adminData">
	 			@Html.EditorFor(model => Model.Locales[item].Attachment1FileId, "Download", new { minimalMode = true })
	 			@Html.ValidationMessageFor(model => model.Locales[item].Attachment1FileId)
			</td>
		 </tr>
		 <tr>
	 		<td class="adminTitle">
	 			@Html.SmartLabelFor(model => model.Locales[item].Attachment2FileId)
			</td>
			<td class="adminData">
	 			@Html.EditorFor(model => Model.Locales[item].Attachment2FileId, "Download", new { minimalMode = true })
				@Html.ValidationMessageFor(model => model.Locales[item].Attachment2FileId)
			</td>
		 </tr>
		 <tr>
	 		<td class="adminTitle">
	 			@Html.SmartLabelFor(model => model.Locales[item].Attachment3FileId)
			</td>
			<td class="adminData">
	 			@Html.EditorFor(model => Model.Locales[item].Attachment3FileId, "Download", new { minimalMode = true })
				@Html.ValidationMessageFor(model => model.Locales[item].Attachment3FileId)
			</td>
		</tr>
        <tr class="wide">
            <td class="adminData">
                @Html.EditorFor(model => Model.Locales[item].Body, "Liquid", new { TemplateName = templateName })
                @Html.ValidationMessageFor(model => model.Locales[item].Body)
            </td>
        </tr>
    </table>
    ,
    @<table class="adminContent">
        <tr>
             <td class="adminTitle">
                 @Html.SmartLabelFor(model => model.EmailAccountId)
             </td>
             <td class="adminData">
                 @Html.DropDownListFor(model => model.EmailAccountId, new SelectList(Model.AvailableEmailAccounts, "Id", "DisplayName"))
                 @Html.ValidationMessageFor(model => model.EmailAccountId)
             </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.To)
            </td>
            <td class="adminData">
                @Html.TextBoxFor(model => model.To)
                @Html.ValidationMessageFor(model => model.To)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.ReplyTo)
            </td>
            <td class="adminData">
                @Html.TextBoxFor(model => model.ReplyTo)
                @Html.ValidationMessageFor(model => model.ReplyTo)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.BccEmailAddresses)
            </td>
            <td class="adminData">
                @Html.TextBoxFor(model => model.BccEmailAddresses)
                @Html.ValidationMessageFor(model => model.BccEmailAddresses)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.Subject)
            </td>
            <td class="adminData">
                @Html.TextBoxFor(model => model.Subject)
                @Html.ValidationMessageFor(model => model.Subject)
            </td>
        </tr>
	 	<tr>
	 		<td class="adminTitle">
	 			@Html.SmartLabelFor(model => model.Attachment1FileId)
	 		</td>
	 		<td class="adminData">
	 			@Html.EditorFor(model => Model.Attachment1FileId, "Download", new { minimalMode = true })
	 			@Html.ValidationMessageFor(model => model.Attachment1FileId)
			</td>
		</tr>
	 	<tr>
	 		<td class="adminTitle">
	 			@Html.SmartLabelFor(model => model.Attachment2FileId)
	 		</td>
	 		<td class="adminData">
	 			@Html.EditorFor(model => Model.Attachment2FileId, "Download", new { minimalMode = true })
	 			@Html.ValidationMessageFor(model => model.Attachment2FileId)
			</td>
		</tr>
	 	<tr>
	 		<td class="adminTitle">
	 			@Html.SmartLabelFor(model => model.Attachment3FileId)
	 		</td>
	 		<td class="adminData">
	 			@Html.EditorFor(model => Model.Attachment3FileId, "Download", new { minimalMode = true })
	 			@Html.ValidationMessageFor(model => model.Attachment3FileId)
			</td>
		</tr>
        <tr class="wide">
            <td class="adminData">
                @Html.EditorFor(model => model.Body, "Liquid", new { TemplateName = templateName })
                @Html.ValidationMessageFor(model => model.Body)
            </td>
        </tr>
    </table>
    ))
}

@helper TabStores()
{
	@Html.Partial("StoreSelector", Model)
}